﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sims3Package;
using Sims3Package.Wrapper;
using Sims3Package.Interface;

namespace PackageEditor.Forms
{
    public partial class Import : Form
    {
        private Package pack;
        private bool replace;
        private bool compress;
        private bool useResourceName;
        NameMapResourceWrapper w;
        ITGIblock TGI;

        public Import(ref Package pack)
        {
            InitializeComponent();
            this.pack = pack;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            IList<string> pathList = this.listPath.Items as IList<string>;
            if (pathList == null || pathList.Count == 0) this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            progressBar.Visible = true; 
            bgwImport.RunWorkerAsync(pathList);
        }

        private void Import_Load(object sender, EventArgs e)
        {
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.Filter = "Package File|*.package";
                open.Multiselect = true;
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (string path in open.SafeFileNames) this.listPath.Items.Add(path);
                    combReplace.SelectedIndex = 0;
                }
                else { this.Dispose(); }
            }
        }

        private void ckbCompress_CheckedChanged(object sender, EventArgs e)
        {
            this.compress = ckbCompress.Checked;
        }

        private void ckbResourceName_CheckedChanged(object sender, EventArgs e)
        {
            this.useResourceName = ckbResourceName.Checked;
        }

        private void combReplace_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.replace = combReplace.SelectedIndex == 0;
        }


        private void bgwImport_DoWork(object sender, DoWorkEventArgs e)
        {
            IList<string> pathList = e.Argument as IList<string>;
            if (pathList == null) return;
            bool replace = this.combReplace.SelectedIndex == 0;
            w = new NameMapResourceWrapper(pack.EntryList.Exists(entry => entry.ResourceType == 0x0166038CU) ? pack.GetResourceStream(pack.EntryList.Find(entry => entry.ResourceType == 0x0166038CU))
            : null);
            TGI = pack.EntryList.Find(entry => entry.ResourceType == 0x0166038CU);
            for (int i = 0; i < pathList.Count; i++)
            {
                
                try
                {
                    using (Package packImp = new Package(pathList[i], false))
                    {
                        this.progressBar.Maximum = packImp.EntryCount;
                        for (int m = 0; m < packImp.EntryCount; m++)
                        {
                            // Report state
                            bgwImport.ReportProgress(m, pathList[i]);

                            if (packImp.EntryList.Exists(entry => entry.ResourceType == 0x0166038CU) && this.ckbResourceName.Checked)
                            {
                                NameMapResourceWrapper wImp = new NameMapResourceWrapper(null);
                                foreach (var kvp in wImp.NameMap) w.NameMap.Add(kvp.Key, kvp.Value);
                            }
                            else
                            {
                                foreach(ResourceEntry entry in packImp.EntryList)
                                {
                                    pack.AddResourceEntry(entry, packImp.GetResourceStream(entry), this.ckbCompress.Checked, replace);
                                }
                            
                            }
                        }
                    }
                }
                catch (Exception ex) { ExceptionBox.Show(ex.ToString()); }


            }
        }

        private void bgwImport_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.Text = string.Format("Importing from {0}", e.UserState);
            this.progressBar.Value = e.ProgressPercentage;
        }

        private void bgwImport_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Text = "Import";
            if (w != null && w.NameMap.Count > 0) pack.ReplaceResource(TGI, w.UnParse(), true);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
